package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
@Autowired
private GraphInfoService graphInfoService;

@Autowired
private RoomAttrValueService roomAttrValueService;

@Autowired
private RoomFacilityService roomFacilityService;

@Autowired
private RoomLabelService roomLabelService;

@Autowired
private RoomLeaseTermService roomLeaseTermService;

@Autowired
private RoomPaymentTypeService roomPaymentTypeService;

@Autowired
private RoomInfoMapper roomInfoMapper;

@Autowired
private GraphInfoMapper graphInfoMapper;

@Autowired
private AttrValueMapper attrValueMapper;

@Autowired
private FacilityInfoMapper facilityInfoMapper;

@Autowired
private LabelInfoMapper labelInfoMapper;

@Autowired
private PaymentTypeMapper paymentTypeMapper;

@Autowired
private LeaseTermMapper leaseTermMapper;

@Autowired
private ApartmentInfoMapper apartmentInfoMapper;

    @Override
    public void customSaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        //1.判断前端传过来有没有id,有就是更新,没有就是保存.
        boolean q = roomSubmitVo.getId()!=null;
        //2.保存房间单表信息,主键回显,方便后续房间id操作
        saveOrUpdate(roomSubmitVo);
        Long id = roomSubmitVo.getId();
        //3.判断q,为true就为更新(删除+保存)
        if (q){
            //删除图片
            LambdaQueryWrapper<GraphInfo> a = new LambdaQueryWrapper<>();
            a.eq(GraphInfo::getItemId,id);
            a.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(a);

            //删除配套
            LambdaQueryWrapper<RoomFacility> b = new LambdaQueryWrapper<>();
            b.eq(RoomFacility::getRoomId,id);
            roomFacilityService.remove(b);

            //删除标签
            LambdaQueryWrapper<RoomLabel> c = new LambdaQueryWrapper<>();
            c.eq(RoomLabel::getRoomId,id);
            roomLabelService.remove(c);

            //删除属性值
            LambdaQueryWrapper<RoomAttrValue> d = new LambdaQueryWrapper<>();
            d.eq(RoomAttrValue::getRoomId,id);
            roomAttrValueService.remove(d);

            //删除支付关系
            LambdaQueryWrapper<RoomPaymentType> e = new LambdaQueryWrapper<>();
            e.eq(RoomPaymentType::getRoomId,id);
            roomPaymentTypeService.remove(e);

            //删除租期关系
            LambdaQueryWrapper<RoomLeaseTerm> f = new LambdaQueryWrapper<>();
            f.eq(RoomLeaseTerm::getRoomId,id);
            roomLeaseTermService.remove(f);
        }

        //保存
        //图片 先判断前端传过来是否有图片,然后再保存.
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo z = GraphInfo.builder().itemId(id).itemType(ItemType.ROOM).name(graphVo.getName()).url(graphVo.getUrl())
                        .build();
                graphInfoList.add(z);

            }
            graphInfoService.saveBatch(graphInfoList);
        }

        //配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            List<RoomFacility> list = new ArrayList<>(facilityInfoIds.size());
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().facilityId(facilityInfoId).roomId(id).build();
                list.add(roomFacility);
            }
            roomFacilityService.saveBatch(list);
        }

        //标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)){
            List<RoomLabel> list = new ArrayList<>(labelInfoIds.size());
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().labelId(labelInfoId).roomId(id).build();
                list.add(roomLabel);
            }
            roomLabelService.saveBatch(list);

        }

        //属性值
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)){
            List<RoomAttrValue> list = new ArrayList<>(attrValueIds.size());
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().attrValueId(attrValueId).roomId(id).build();
                list.add(roomAttrValue);
            }

            roomAttrValueService.saveBatch(list);
        }

        //支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)){
            List<RoomPaymentType> list = new ArrayList<>(paymentTypeIds.size());
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().paymentTypeId(paymentTypeId).roomId(id).build();
                list.add(roomPaymentType);
            }

            roomPaymentTypeService.saveBatch(list);
        }

        //租期关系
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)){
            List<RoomLeaseTerm> list = new ArrayList<>(leaseTermIds.size());
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().leaseTermId(leaseTermId).roomId(id).build();
                list.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(list);
        }
    }

    @Override
    public void queryPage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
         roomInfoMapper.queryPage(page,queryVo);
    }

    @Override
    public RoomDetailVo queryDetailById(Long id) {
        //1.查询roomInfo单表,并且判断是否被其他管理员删除
        RoomInfo roomInfo = getById(id);
        if(ObjectUtils.isEmpty(roomInfo)){
            return null;
        }
        //2.查询图片
        List<GraphVo> graphVoList = graphInfoMapper.queryByItemId(id,ItemType.ROOM);

        //3.查询属性
        List<AttrValueVo> attrValueVoList = attrValueMapper.queryByRoomId(id);

        //4.查询配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.queryByRoomId(id);

        //5.查询标签
        List<LabelInfo> labelInfoList = labelInfoMapper.queryByRoomId(id);

        //6.查询支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.queryByRoomId(id);

        //7.查询可选租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.queryByRoomId(id);

        //8.查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        //9.合并到RoomDetailVo
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    @Override
    public void customRemoveById(Long id) {
        //1.删除房间,待判定状态,判定是否已经在住的状态
        removeById(id);
        //2.删除图片
        LambdaQueryWrapper<GraphInfo> a = new LambdaQueryWrapper<>();
        a.eq(GraphInfo::getItemId,id);
        a.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoService.remove(a);
        //3.删除配套
        LambdaQueryWrapper<RoomFacility> b = new LambdaQueryWrapper<>();
        b.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(b);
        //4.删除标签
        LambdaQueryWrapper<RoomLabel> c = new LambdaQueryWrapper<>();
        c.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(c);
        //5.删除属性
        LambdaQueryWrapper<RoomAttrValue> d = new LambdaQueryWrapper<>();
        d.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(d);
        //6.删除支付方式
        LambdaQueryWrapper<RoomAttrValue> e = new LambdaQueryWrapper<>();
        e.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(e);
        //7.删除租约关系
        LambdaQueryWrapper<RoomLeaseTerm> f = new LambdaQueryWrapper<>();
        f.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(f);

    }
}




